import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    meta:{title:'index'},
    component: ()=>import('../views/Index.vue'),
    children:[
      {
        path:'/index/home',
        name:'home',
        meta:{title:'首页'},
        component:()=>import('../views/Index/Home.vue')
      },
      {
        path:'/index/order',
        name:'order',
        meta:{title:'订单'},
        component:()=>import('../views/Index/Order.vue')
      },
      {
        path:'/index/tuan',
        name:'tuan',
        meta:{title:'爆爆团'},
        component:()=>import('../views/Index/Tuan.vue')
      },
      {
        path:'/index/mine',
        name:'mine',
        meta:{title:'我的'},
        component:()=>import('../views/Index/Mine.vue')
      },
      {
        path:'/index',
        redirect:'/index/home'
      }
    ]
  },
  {
    path:'/login',
    name:'login',
    meta:{title:'登录'},
    component:()=>import('../views/Login.vue')
  },
  {
    path:'/register',
    name:'register',
    meta:{title:'注册'},
    component:()=>import('../views/Register.vue')
  },
  {
    path:'/',
    redirect:'/index'
  },
  {
    path:'*',
    component:()=>import('../views/NotFound.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  document.title = to.meta.title;
  var token = localStorage.getItem('token');
  if(to.path == '/index/order'|| to.path == '/index/mine'){
    if(token){
      next();
    }else{
      next('/login')
    }
  }else{
    next();
  }
})

export default router
